package com.heo.protocol.packet;

import org.apache.log4j.Logger;
import com.heo.util.ByteHelper;

public class LoginReq extends BasePacket
{
  private int BODY_LENGTH = 35;// 包体长35个字节
  private static final String PACKET_NAME = "[登录请求包]";

  private static Logger log = Logger.getLogger(LoginReq.class);

  private String loginName = null;
  private short loginType = 0x00;//默认登录类型为imei号登录
  private short deviceType = 0x0000;//设备类型
  private String devicePass = "0000";
  private String firmwareVer;
  private String protocolVer;

  /**
   * 通过网络传过来的流构建对象
   * 
   * @param totalLength
   * @param commandId
   * @param sequenceId
   * @param body
   * @throws Exception
   */
  public LoginReq(String logHeader, SocketPacket packet) throws Exception
  {
    super.copyPacket(logHeader, PACKET_NAME, packet);
    makeBody();
  }

  /**
   * 通过字符串构建对象
   * 
   * @param imei
   * @param simId
   * @param devicePass
   * @param firmwareVer
   * @param protocolVer
   * @throws Exception
   */
  public LoginReq(String loginName, short loginType, short deviceType, String devicePass, String firmwareVer, String protocolVer) throws Exception
  {
    this.loginName = loginName;
    this.deviceType = deviceType;
    this.loginType = loginType;
    this.devicePass = devicePass;
    this.firmwareVer = firmwareVer;
    this.protocolVer = protocolVer;
    super.totalLength = (short) (HEAD_LENGTH + BODY_LENGTH);
    super.commandId = DeviceCommand.LOGIN_REQ;
    super.sequenceId = 0x0001;
    super.packetName = PACKET_NAME;
  }

  @Override
  public byte[] getPacketBody()
  {
    //要把登录名称补足20位
    byte[] loginNameArray = ByteHelper.fillByteArray(loginName.getBytes(),(byte)0x00,20);
    
    return ByteHelper.margeByte(loginNameArray, new byte[]{(byte)loginType}, ByteHelper.shortToByteArray(deviceType), devicePass.getBytes(), firmwareVer.getBytes(), protocolVer.getBytes(), null, null, null, null);
  }

  @Override
  public void makeBody() throws Exception
  {
    if (packetBody.length != BODY_LENGTH)
    {
      log.error(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
      throw new Exception(super.logHeader + super.packetName + "数据包长度错误。包体长度应该为：" + BODY_LENGTH + "个字节。");
    }
    loginName = ByteHelper.byteToString(packetBody, 0, 20, (byte) 0);
    loginType = ByteHelper.byteToShort(packetBody, 20, 1);
    deviceType = ByteHelper.byteToShort(packetBody, 21, 2);
    devicePass = ByteHelper.byteToString(packetBody, 23, 4, (byte) 0);
    firmwareVer = ByteHelper.byteToString(packetBody, 27, 4, (byte) 0);
    protocolVer = ByteHelper.byteToString(packetBody, 31, 4, (byte) 0);
    log.debug(super.logHeader + super.packetName + "数据包解析成功。内容：[loginName]" + loginName + " [loginType]" + loginType + "(0：imei登录，1：MAC登录，2：SN登录) [deviceType]" + deviceType + " [devicePass]" + devicePass + " [firmwareVer]" + firmwareVer + " [protocolVer]" + protocolVer);
  }

  public static void main(String[] args)
  {
    String iemi = "111111111111111";
    short loginType = 0x00;
    short deviceType = 0x0001;
    String devicePass = "abcd";
    String firmwareVer = "1.00";
    String protocolVer = "1.00";
    try
    {
      LoginReq loginReq = new LoginReq(iemi,loginType, deviceType, devicePass, firmwareVer, protocolVer);
      System.out.println(loginReq.toString());
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }

  public String getDevicePass()
  {
    return devicePass;
  }

  public void setDevicePass(String devicePass)
  {
    this.devicePass = devicePass;
  }

  @Override
  public int getTotalLength()
  {
    return super.HEAD_LENGTH + BODY_LENGTH;
  }

  public short getDeviceType()
  {
    return deviceType;
  }

  public void setDeviceType(short deviceType)
  {
    this.deviceType = deviceType;
  }

  public String getFirmwareVer()
  {
    return firmwareVer;
  }

  public void setFirmwareVer(String firmwareVer)
  {
    this.firmwareVer = firmwareVer;
  }

  public String getProtocolVer()
  {
    return protocolVer;
  }

  public void setProtocolVer(String protocolVer)
  {
    this.protocolVer = protocolVer;
  }

  public short getLoginType()
  {
    return loginType;
  }

  public void setLoginType(short loginType)
  {
    this.loginType = loginType;
  }

  public String getLoginName()
  {
    return loginName;
  }

  public void setLoginName(String loginName)
  {
    this.loginName = loginName;
  }

}
